<%@ page import="databean.User"%>
<%@ page import="databean.Tokbox"%>
<%@ page import="java.util.*,java.io.*;"%>
<jsp:include page="include-header.jsp"/>
<script type="text/javascript" charset="utf-8">
			//global scripts
			$(document).ready(function() {
				$('#roomlist').dataTable(); //tablesorting
			});

</script>
<%
	User user = (User) request.getAttribute("user");
	ArrayList<User> users = (ArrayList<User>) request.getAttribute("users");
	ArrayList<Tokbox> tokboxes = (ArrayList<Tokbox>) request.getAttribute("tokboxes");
	
	String name = user.getFirstName() + " " + user.getLastName();

%>

<div id="content" class="full_column">
	<div id="content_header">Active Meeting Rooms</div>
	
	<div id="content_padded">
	<div id="left_inner" style="width: 28%">
	<%
	//User user = (User) request.getAttribute("user");
	
	//String name = user.getFirstName() + " " + user.getLastName();
	long creator = user.getUserID();

%>
<!-- create a video conference -->
	<h4>Create a Conference Room</h4>
		<div id="profileInfo_details" style="">
		<jsp:include page="user-error-list.jsp" />
		<form method="post" action="video-create-conf.do">
		<input type="hidden" name="creatorID" id="creatorID" value="<%= creator %>" readonly="readonly" />
		<div id="profile_info_text_left">
		<label>Conference Name: </label><br>
		<label>Attendees: </label><br>
		<%
			for (int i=0; i<users.size(); i++) {
				User dbUser = users.get(i);
		%>
			<br>  
		<%
			}
		%>
		<label>Note Taker: </label><br>
		
		</div>
		<div id="profile_info_text_right">
		<label><input type="text" name="confName" id="confName" value="" /></label><br>
		<label>
		<%
			for (int i=0; i<users.size(); i++) {
				User dbUser = users.get(i);
		%>
			<input type="checkbox" name="attendeesID[<%=i%>]" value="<%=dbUser.getUserID()%>" /> <%= dbUser.getFirstName() + " " + dbUser.getLastName() %><br>
		<%
			}
		%>
		</label><br>
		
		
		<label>
		<select name="noteTakerID">
		<%
			for (int i=0; i<users.size(); i++) {
				User dbUser = users.get(i);
		%>
		  <option value="<%=dbUser.getUserID()%>"> <%= dbUser.getFirstName() + " " + dbUser.getLastName() %></option>
		<%
			}
		%>
		</select>
		</label><br>
		
		
		
		</div>

	    
	    <input style="width:100%; margin: auto" type="submit" name="button" id="button" class="awesome blue medium" value="Create Conference" />
		
	    </form>
		
		</div>
		
		
		
	</div>
	
	
	
	<div id="right_inner" style="width: 70%; padding-left: 14px;">
	<h4>List of Active Conference Rooms </h4>
	<table class="display" id="roomlist">	
						<thead>
							<tr>
								<th>Creator</th>
								<th>Conference Name</th>
								<th>Attendees</th>
								<th>Note Taker</th>
								<th>Notes</th>
								<th>Action</th>
							</tr>
						</thead>	
						<tbody>
<%
	for (int i=0; i<tokboxes.size(); i++) {
		
		Tokbox tokbox = tokboxes.get(i);
		String aNames = tokbox.getAttendeesNames();
		StringTokenizer st=new StringTokenizer(aNames,",");
%>					
							<tr class="inboxContainer" id="1">
								<td><%= tokbox.getCreatorName() %></td>
								<td><%= tokbox.getConfName() %></td>
								<td><div class="messageInfo2">
								<%
								while(st.hasMoreTokens())
								   {
									String token=st.nextToken();%>
									<a href="#"><%=token%></a>
								<% } %>
								</div>
								</td>
								<td><%= tokbox.getNoteTakerName() %></td>
								
								
							
<%
		//check if this person is authorized to join this particular video conference
		boolean authorized = false;
		
		if (Long.parseLong(tokbox.getCreatorID())==user.getUserID()) {
			authorized = true;
		} else if (Long.parseLong(tokbox.getNoteTakerID())==user.getUserID()) {
			authorized = true;
		} else {
		
			if (tokbox.getAttendeesID().length()==1) {
				if (Long.parseLong(tokbox.getAttendeesID())==user.getUserID()) {
	    			authorized = true;
	    		}
			} else {
				String[] attendeesID = tokbox.getAttendeesID().split(",");
				
				for (int j=0; j<attendeesID.length; j++) {
		    		
		    		if (!attendeesID[j].equals("")) {
			    		if (Long.parseLong(attendeesID[j])==user.getUserID()) {
			    			authorized = true;
			    		}
		    		}
		    		
		    		if (authorized) j=attendeesID.length;
		    		
		    	}
			}
			
		}
		
		if (authorized) {
	%>
			<td><%= tokbox.getNotes() %></td>
			<td><a href="video-view-conf.do?confName=<%= tokbox.getConfName() %>&type=join" class="awesome orange small">Join</a></td>
	<%
		} else {
	%>
			<td></td>
			<td>Private</td>
		<%
		}
	%>
	</td>
<%
	}
%>					
					</tr>	
		</tbody>			
	</table>
	</div>
	</div>
</div>

<jsp:include page="include-footer.jsp"/>
	
